<template>
<a-modal width="80%" v-model:visible="visible" fullscreen :footer="false">
  <template #title>维护渠道数据</template>
  <ma-crud :options="options" :columns="columns" ref="crudRef">
    <template #tableAfterButtons>
      <a-popconfirm
                content="远程拉去会覆盖本地数据，是否继续？"
                position="bottom"
                @ok="pull()"
            >
      <a-button type="primary" :auth="['logistics:supplierChannel:pull']">
          <template #icon>
            <icon-refresh/>
          </template>
          远程拉取
        </a-button>
      </a-popconfirm>
      <button-group :button-groups="buttonGroups" :crud-ref="crudRef" :selected="selected" />
    </template>
  </ma-crud>
</a-modal>
</template>
<script setup>
import { ref, reactive,computed } from 'vue'
import supplierChannel from '@/api/logistics/supplierChannel'
import ButtonGroup from "@cps/actions/buttonGroup.vue";
const crudRef = ref()
const visible = ref(false)
const supplier_id = ref()
const selected = computed(()=>crudRef.value?.selecteds)

const open = (id)=>{
  supplier_id.value = id
  crudRef.value.requestData()
  visible.value = true
}

const buttonGroups = [
  {
    title:'设置',
    api:supplierChannel.setStatus,
    displayDataIndex:"label",
    form: {
      columns: [
        {
          title: '操作', dataIndex: 'status', formType: 'radio', dict: {
            data: [
              {label: '关闭', value: 0},
              {label: '开启', value: 1},
            ]
          },
        },
      ]
    },
  },
]

const pull = async ()=>{
   let res = await supplierChannel.pull({supplier_id:supplier_id.value})
   if(res.success){
    crudRef.value.refresh()
   }
}
const options = reactive({
  autoRequest: false,
  id: 'supplier_channel',
  rowSelection: {showCheckedAll: true},
  operationColumn: true,
  operationColumnWidth: 160,
  beforeRequest: (params) => params.supplier_id = supplier_id.value,
  api: supplierChannel.getList,
  beforeAdd: (form) => {
    form.supplier_id = supplier_id.value
    return true
  },
  add: {
    show: true,
    api: supplierChannel.save,
    auth: ['logistics:supplierChannel:save']
  },
  edit: {
    show: true,
    api: supplierChannel.update,
    auth: ['logistics:supplierChannel:update']
  },
  delete: {
    show: true,
    api: supplierChannel.deletes,
    auth: ['logistics:supplierChannel:delete']
  }
})

const columns = reactive([
  {
    title: "主键",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入主键"
    }
  },
  {
    title: "名称",
    dataIndex: "label",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入标题"
    }
  },
  {
    title: "值",
    dataIndex: "value",
    formType: "input",
    commonRules: {
      required: true,
      message: "请输入值"
    }
  },
  {
    title: "承运商",
    dataIndex: "carrier",
    formType: "input",
  },
  {
    title: "状态",
    dataIndex: "status",
    formType: "radio",
    dict:{
      data:[
        {label:'启用',value:1},
        {label:'禁用',value:0}
      ],
      translation: true
    },
    type:'button'
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "备注",
    dataIndex: "remark",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
defineExpose({open})
</script>
<script> export default { name: 'logistics:supplierChannel' } </script>